/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.amqp.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.tianyun.cloud.utils.JsonUtils;
import org.tianyun.cloud.utils.XmlUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 消息消费者 - 基类
 *
 * @auther ebert_chan
 */
public abstract class BaseListener {

    /**
     * 日志
     */
    public Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 消息转化为字符串消息体
     *
     * @param message
     * @return
     * @auther ebert_chan
     */
    public String messageToString(Message message) {
        String receiveMsg = null;
        MessageProperties messageProperties = message.getMessageProperties();
        String encoding = "UTF-8";
        if (messageProperties != null) {
            if (StringUtils.isNotEmpty(messageProperties.getContentEncoding())) {
                encoding = messageProperties.getContentEncoding();
            }
        }
        try {
            receiveMsg = new String(message.getBody(), encoding);
        } catch (UnsupportedEncodingException e) {
            log.error("UnsupportedEncodingException", e);
        }
        log.info("receive: {}", receiveMsg);
        return receiveMsg;
    }

    /**
     * json消息转换为接收对象
     *
     * @param message
     * @param valueType
     * @param parameterClasses
     * @return
     * @auther ebert_chan
     */
    public <T> T messageJsonToObject(Message message, Class<?> valueType, Class<?>... parameterClasses) {
        T dto = null;
        try {
            String receiveMsg = messageToString(message);
            dto = JsonUtils.parse(receiveMsg, valueType, parameterClasses);
        } catch (Exception e) {
            log.error("json parse exception.", e);
        }
        return dto;
    }

    /**
     * xml消息转换为接收对象
     *
     * @param message
     * @param valueType
     * @param parameterClasses
     * @return
     * @auther ebert_chan
     */
    public <T> T messageXmlToObject(Message message, Class<?> valueType, Class<?>... parameterClasses) {
        T dto = null;
        try {
            String receiveMsg = messageToString(message);
            dto = XmlUtils.parse(receiveMsg, valueType, parameterClasses);
        } catch (Exception e) {
            log.error("xml parse exception.", e);
        }
        return dto;
    }

    /**
     * 确认成功消费
     *
     * @param channel
     * @param deliveryTag
     * @auther ebert_chan
     */
    public void ack(Channel channel, long deliveryTag) {
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            log.error("basicAck Excepton", e);
        }
    }

    /**
     * 消息重回队列
     *
     * @param channel
     * @auther ebert_chan
     */
    public void recover(Channel channel) {
        try {
            channel.basicRecover(true);
        } catch (IOException e) {
            log.error("basicRecover Excepton", e);
        }
    }

}
